package my.geraltigas.week6_springboot.controller;

import my.geraltigas.week6_springboot.entity.Goods;
import my.geraltigas.week6_springboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @PostMapping("/goods")
    public ResponseEntity<Void> addGoods(@RequestBody Goods goods) {
        goodsService.add(goods);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/goods/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable int id) {
        goodsService.delete(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/goods")
    public ResponseEntity<Void> updateGood(@RequestBody Goods goods) {
        goodsService.update(goods);
        return ResponseEntity.ok().build();
    }

    @GetMapping(value="/goods/{id}",produces="application/json;charset=UTF-8")
    public ResponseEntity<Goods> getGoods(@PathVariable int id) {
        Goods goods = goodsService.get(id);
        return ResponseEntity.ok(goods);
    }
    @GetMapping(value="/goods",produces="application/json;charset=UTF-8")
    public ResponseEntity<List<Goods>> listGoods() {
        List<Goods> goods = goodsService.list();
        return ResponseEntity.ok(goods);
    }
}
